package org.honeyweb.gsoc.dao.hibernate3;

import java.util.ArrayList;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.honeyweb.gsoc.dao.RunsDao;
import org.honeyweb.gsoc.model.Runs;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public class RunsDaoImpl extends HibernateDaoSupport implements RunsDao {

	public ArrayList<Runs> getRuns(int userId){
		if (0 == userId ) {
			throw new IllegalArgumentException("User is mandatory. Null value is forbidden.");
		}
		try {
			logger.info("get Runs with id: "+ userId);
			Session session = getHibernateTemplate().getSessionFactory().getCurrentSession();
			// create a new criteria
			Criteria crit = session.createCriteria(Runs.class);
			crit.add(Restrictions.eq("userId", userId));
			
			ArrayList<Runs> urlsArray = (ArrayList<Runs>)crit.list();
			return urlsArray;
		}
		catch(DataAccessException e) {
			// Critical errors : database unreachable, etc.
			logger.error("Exception - DataAccessException occurs : "+e.getMessage()
					+" on complete getOrganizations().");
			return null;
		}
	}
	
}
